package leetcode.code986;

import leetcode.helper.HelpDebug;

public class Temp extends Solution986 {

	@Override
	public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
		if (firstList == null || secondList == null || firstList.length == 0 || secondList.length == 0)
			return new int[][] {};
		int fx = firstList.length;
		int sx = secondList.length;
		int max = firstList[fx - 1][1] > secondList[sx - 1][1] ? firstList[fx - 1][1] + 1 : secondList[sx - 1][1] + 1;
		int[] together = new int[max];
		for (int i = 0; i < fx; i++) {
			together[firstList[i][0]] += (i + 1);
			if (firstList[i][1] + 1 < max)
				together[firstList[i][1] + 1] -= (i + 1);
		}
		for (int i = 0; i < sx; i++) {
			together[secondList[i][0]] += (i + 1);
			if (secondList[i][1] + 1 < max)
				together[secondList[i][1] + 1] -= (i + 1);
		}
		int part = 0;
		int last = 1;
		for (int i = 0; i < max; i++) {
			if (i > 0) {
				together[i] += together[i - 1];
			}
			if (together[i] > last) {
				part++;// 增加一段
			}
			last = Math.max(last, together[i]);
		}
		int[][] result = new int[part][2];
		HelpDebug.printObject(together);
		part = 0;
		last = 1;
		for (int i = 0; i < max; i++) {
			if (together[i] > last) {
				result[part][0] = i;
				result[part][1] = i;
				part++;// 增加一段
			} else if (together[i] == last && last != 1) {
				result[part - 1][1] = i;
			}
			last = Math.max(last, together[i]);
		}
		return result;
	}

	public static void main(String[] args) {
		Temp so = new Temp();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		so.debug5();
	}

}
